import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import GlobalData from "../../Script/Common/GlobalData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoseUI extends yyComponent {

    /**场景/UI类型 */
    public get uiType() { return GlobalEnum.UI.loseUI; }

    public init(data?) {
        if (undefined !== data) {
            this.setData(data);
        }
    }
    protected setData(data) {
        this.btnVideo.active = data.canResurgence;
        if (!!data.canResurgence) {
            this.nowTime = 0;
            this.barSpr.fillRange = 1 - (this.nowTime / this.showTime);
        }
    }

    protected onEvents() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.onNodeTouchEnd, this);
        this.on(EventType.LevelEvent.stopJoy, this.onStopJoy, this);
    }

    public reset() {

    }

    public show(data?: { data: any, config: any }) {
        this.reset();
        this.node.active = true;
        if (undefined !== data && undefined !== data.data) {
            this.setData(data.data);
        }
        this.onEvents();
    }
    update(dt) {
        if (this.btnVideo.active && this.nowTime <= this.showTime) {
            this.nowTime += dt;
            this.barSpr.fillRange = 1 - (this.nowTime / this.showTime);
            if (this.nowTime >= this.showTime) {
                this.btnVideo.active = false;
            }
        }
    }

    @property(cc.Node)
    btnVideo: cc.Node = null;
    @property(cc.Sprite)
    barSpr: cc.Sprite = null;
    nowTime: number = 0;
    showTime = 5;

    protected onContinueGame() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.enterLobby();
    }

    protected onBtnResurgence() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.resurgenceByVideo();
    }
    protected resurgenceByVideo() {
        this.emit(EventType.Common.SDKEvent.showVideo, {
            success: () => {
                this.emit(EventType.Common.UIEvent.exit, this.uiType);
                this.emit(EventType.LevelEvent.resurgence);
            },
        });
    }

    protected enterLobby() {
        this.emit(EventType.Common.PlayerDataEvent.trySkinEnd);
        this.emit(EventType.Common.UIEvent.exit, this.uiType);
        this.emit(EventType.Common.GameFlow.enterGameFlow, GlobalEnum.Common.GameFlowStep.recommendDialog);
    }

    protected onBtnMoreGame() {
        this.emit(EventType.Common.SDKEvent.showAppBox);
    }

    protected onBtnShare() {
        this.emit(EventType.Common.SDKEvent.shareToFriends);
    }


    protected onStopJoy(p: cc.Vec2) {
        if (p.y < 320) {
            this.emit(EventType.Common.HeyGameSDK.clickNativeAd);
        }
    }

    protected onNodeTouchEnd(e: cc.EventTouch) {
        let p = e.getUILocation();
        this.onStopJoy(p);
    }
}
